package com.wrb.pwd.component;

import com.wrb.pwd.model.User;
import com.wrb.pwd.model.ValidationQuestion;
import com.wrb.pwd.service.UserService;
import com.wrb.pwd.utils.MD5Utils;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author wrb
 * @date 2019/9/17 14:01
 */
public class UserRecordPanel extends JPanel {

	private UserService userService = UserService.getInstance();

	private JLabel lbPlatform, lbName, lblPwd, lbPhone, lbEmail, lbAuxEmail, lbRemark, lbIMsg,
			lbAns1, lbAns2, lbAns3, lbQue1, lbQue2, lbQue3;

	//声明文本框
	private JTextField txtPlatform, txtName, txtPwd, txtEmail, txtPhone, txtAuxEmail, txtRemark,
			txtAns1, txtAns2, txtAns3, txtQue1, txtQue2, txtQue3;

	private JButton btnReg, btnCancel, btnGenerate;

	private String checkRepeat = "";

	private UserRecordPanel() {
		super(null);

		//实例化5个标签
		lbPlatform = new JLabel("平   台");
		lbName = new JLabel("用户名");
		lblPwd = new JLabel("密   码");
		lbPhone = new JLabel("手机号码");
		lbEmail = new JLabel("邮   箱");
		lbAuxEmail = new JLabel("辅助邮箱");
		lbRemark = new JLabel("备   注");
		lbQue1 = new JLabel("问 题 1");
		lbAns1 = new JLabel("答 案 1");
		lbQue2 = new JLabel("问 题 2");
		lbAns2 = new JLabel("答 案 2");
		lbQue3 = new JLabel("问 题 3");
		lbAns3 = new JLabel("答 案 3");

		//显示信息的标签
		lbIMsg = new JLabel();
		//设置标签的文字是红色
		lbIMsg.setForeground(Color.RED);

		txtPlatform = new JTextField(20);
		txtName = new JTextField(20);
		txtPwd = new JTextField(20);
		txtPwd.setEditable(false);
		txtPhone = new JTextField(20);
		txtEmail = new JTextField(20);
		txtAuxEmail = new JTextField(20);
		txtRemark = new JTextField(20);
		txtAns1 = new JTextField(20);
		txtQue1 = new JTextField(20);
		txtAns2 = new JTextField(20);
		txtQue2 = new JTextField(20);
		txtAns3 = new JTextField(20);
		txtQue3 = new JTextField(20);

		//创建两个按钮
		btnReg = new JButton("记录");
		btnCancel = new JButton("清空");
		btnGenerate = new JButton("生成密码");

		//生成密码
		btnGenerate.addActionListener(e -> {
			//设置标签的文字是红色
			lbIMsg.setText("");
			lbIMsg.setForeground(Color.RED);

			String platformStr = txtPlatform.getText();
			if (platformStr == null || "".equals(platformStr.trim())) {
				lbIMsg.setText("平台不能为空");
				return;
			}
			String pwdStr = userService.generatePassword(platformStr, "");
			txtPwd.setText(pwdStr);
			lbIMsg.setForeground(Color.GREEN);
			lbIMsg.setText("生成密码成功");
		});

		//注册监听
		btnReg.addActionListener(e -> {
			//设置信息标签为空 清楚原来的历史信息
			lbIMsg.setText("");
			//设置标签的文字是红色
			lbIMsg.setForeground(Color.RED);

			//获取平台
			String platformStr = txtPlatform.getText();
			if (platformStr == null || "".equals(platformStr.trim())) {
				lbIMsg.setText("平台不能为空");
				return;
			}
			//获取用户输入的用户名
			String nameStr = txtName.getText();
			if (nameStr == null || "".equals(nameStr.trim())) {
				lbIMsg.setText("用户名不能为空");
				return;
			}
			String pwdStr = txtPwd.getText();
			if (pwdStr == null || "".equals(pwdStr.trim())) {
				lbIMsg.setText("密码不能为空");
				return;
			}
			User user = new User();
			user.setId(String.valueOf(System.currentTimeMillis()));
			user.setPlatform(platformStr);
			user.setName(nameStr);
			user.setPassword(pwdStr);
			String phoneStr = txtPhone.getText();
			if (phoneStr != null && !"".equals(phoneStr.trim())) {
				user.setPhone(phoneStr);
			}
			String emailStr = txtEmail.getText();
			if (emailStr != null && !"".equals(emailStr.trim())) {
				user.setEmail(emailStr);
			}
			String auxiliaryEmailStr = txtAuxEmail.getText();
			if (auxiliaryEmailStr != null && !"".equals(auxiliaryEmailStr.trim())) {
				user.setAuxiliaryEmail(auxiliaryEmailStr);
			}
			String remarkStr = txtRemark.getText();
			if (remarkStr != null && !"".equals(remarkStr.trim())) {
				user.setRemark(remarkStr);
			}
			List<ValidationQuestion> list = new ArrayList<>();
			String ans1 = txtAns1.getText();
			String que1 = txtQue1.getText();
			if (addValidationQuestion(list, 1, ans1, que1)) {
				return;
			}
			String ans2 = txtAns2.getText();
			String que2 = txtQue2.getText();
			if (addValidationQuestion(list, 2, ans2, que2)) {
				return;
			}
			String ans3 = txtAns3.getText();
			String que3 = txtQue3.getText();
			if (addValidationQuestion(list, 3, ans3, que3)) {
				return;
			}
			if (checkRepeat.equals(platformStr + nameStr)) {
				lbIMsg.setText("请勿重复记录");
				return;
			} else {
				checkRepeat = platformStr + nameStr;
			}
			Date now = new Date();
			user.setCreateTime(now);
			user.setUpdateTime(now);
			if (list.size() != 0) {
				user.setValidationQuestionList(list);
			}

			boolean isSuccess = userService.insertUser(user);
			if (isSuccess) {
				JOptionPane.showMessageDialog(this, "记录成功！", "提示", JOptionPane.PLAIN_MESSAGE);
				this.lbIMsg.setText("");
			} else {
				JOptionPane.showMessageDialog(this, "记录失败！", "错误 ", JOptionPane.ERROR_MESSAGE);
			}


		});

		//清空按钮的事件处理
		btnCancel.addActionListener(e -> this.init());

		lbPlatform.setBounds(30, 30, 60, 25);
		txtPlatform.setBounds(95, 30, 180, 25);
		lbName.setBounds(30, 60, 60, 25);
		txtName.setBounds(95, 60, 180, 25);
		lblPwd.setBounds(30, 90, 60, 25);
		txtPwd.setBounds(95, 90, 180, 25);
		btnGenerate.setBounds(280, 90, 120, 25);
		lbPhone.setBounds(30, 120, 60, 25);
		txtPhone.setBounds(95, 120, 180, 25);
		lbEmail.setBounds(30, 150, 60, 25);
		txtEmail.setBounds(95, 150, 180, 25);
		lbAuxEmail.setBounds(30, 180, 60, 25);
		txtAuxEmail.setBounds(95, 180, 180, 25);
		lbRemark.setBounds(30, 210, 60, 25);
		txtRemark.setBounds(95, 210, 180, 25);
		lbQue1.setBounds(30, 240, 60, 25);
		txtQue1.setBounds(95, 240, 180, 25);
		lbAns1.setBounds(30, 270, 60, 25);
		txtAns1.setBounds(95, 270, 180, 25);
		lbQue2.setBounds(30, 300, 60, 25);
		txtQue2.setBounds(95, 300, 180, 25);
		lbAns2.setBounds(30, 330, 60, 25);
		txtAns2.setBounds(95, 330, 180, 25);
		lbQue3.setBounds(30, 360, 60, 25);
		txtQue3.setBounds(95, 360, 180, 25);
		lbAns3.setBounds(30, 390, 60, 25);
		txtAns3.setBounds(95, 390, 180, 25);


		lbIMsg.setBounds(60, 420, 180, 25);
		btnReg.setBounds(140, 450, 60, 25);
		btnCancel.setBounds(210, 450, 60, 25);

		//添加所有组件
		this.add(lbPlatform);
		this.add(txtPlatform);
		this.add(lbName);
		this.add(txtName);
		this.add(lblPwd);
		this.add(txtPwd);
		this.add(btnGenerate);
		this.add(lbPhone);
		this.add(txtPhone);
		this.add(lbEmail);
		this.add(txtEmail);
		this.add(lbAuxEmail);
		this.add(txtAuxEmail);
		this.add(lbRemark);
		this.add(txtRemark);
		this.add(lbIMsg);
		this.add(btnReg);
		this.add(btnCancel);
		this.add(lbAns1);
		this.add(lbQue1);
		this.add(txtAns1);
		this.add(txtQue1);
		this.add(lbAns2);
		this.add(lbQue2);
		this.add(txtAns2);
		this.add(txtQue2);
		this.add(lbAns3);
		this.add(lbQue3);
		this.add(txtAns3);
		this.add(txtQue3);
	}

	private boolean addValidationQuestion(List<ValidationQuestion> list, int pos, String ans, String que) {
		boolean bool1 = (ans != null && !"".equals(ans.trim()));
		boolean bool2 = (que != null && !"".equals(que.trim()));
		if (bool1 != bool2) {
			if (bool1) {
				lbIMsg.setText("答案" + pos + "已填写，问题" + pos + "不能为空");
			} else {
				lbIMsg.setText("问题" + pos + "已填写，答案" + pos + "不能为空");
			}
			return true;
		}
		if (bool1) {
			ValidationQuestion validationQuestion = new ValidationQuestion();
			validationQuestion.setQuestion(que);
			validationQuestion.setAnswer(ans);
			list.add(validationQuestion);
		}
		return false;
	}

	public void setHomeButton(JButton jbHome) {
		jbHome.setBounds(280, 450, 60, 25);
		this.add(jbHome);
	}

	public void init() {
		this.txtPlatform.setText("");
		this.txtName.setText("");
		this.txtPwd.setText("");
		this.txtPhone.setText("");
		this.txtEmail.setText("");
		this.txtAuxEmail.setText("");
		this.txtRemark.setText("");
		this.lbIMsg.setText("");
		this.txtAns1.setText("");
		this.txtAns2.setText("");
		this.txtAns3.setText("");
		this.txtQue1.setText("");
		this.txtQue2.setText("");
		this.txtQue3.setText("");
	}

	public static UserRecordPanel getInstance() {
		return Inner.INSTANCE;
	}

	private static class Inner {
		private static final UserRecordPanel INSTANCE = new UserRecordPanel();
	}

}
